﻿/*----------------------------------------------------------------
//  <copyright file="MessageController.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2023-11-22 15:15</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud.Api.Message.Areas.Admin.Controllers.Message
{
    #region "消息管理"
    /// <summary>
    /// 消息管理
    /// </summary>
    [Description("消息管理")]
    [ModuleInfo(Code = "Message", Name = "消息管理", OrderNo = 1, Position = "Msg", PositionName = "消息模块")]
    public class MessageController : AdminControllerBase
    {
        #region "属性"
        #region "获取 消息服务"
        /// <summary>
        /// 获取 消息服务
        /// </summary>
        protected IMessageService MessageService => ServiceProvider.GetService<IMessageService>();
        #endregion

        #endregion

        #region "Action方法"
        #region "读取消息"
        /// <summary>
        /// 读取消息
        /// </summary>
        /// <param name="request">分页请求</param>
        /// <returns>消息页列表</returns>
        [HttpPost]
        [ModuleInfo]
        [Description("读取")]
        public virtual PageData<MessageOutputDto> Read(PageRequest request)
        {
            Check.NotNull(request, nameof(request));
            IFunction function = this.GetExecuteFunction();

            Expression<Func<MessageEntity, bool>> predicate = FilterService.GetExpression<MessageEntity>(request.FilterGroup);
            request.AddDefaultSortCondition(new SortCondition("CreatedTime", ListSortDirection.Descending));
            var page = CacheService.ToPageCache<MessageEntity, MessageOutputDto>(MessageService.Messages, predicate, request.PageCondition, function);

            return page.ToPageData();
        }
        #endregion
        #region "读取消息接收者"
        /// <summary>
        /// 读取消息接收者
        /// </summary>
        /// <param name="request">分页请求</param>
        /// <returns>消息页列表</returns>
        [HttpPost]
        [ModuleInfo]
        [DependOnFunction(nameof(Read))]
        [Description("读取接收者")]
        public virtual PageData<MessageReceiverOutputDto> ReadReceiver(PageRequest request)
        {
            Check.NotNull(request, nameof(request));
            IFunction function = this.GetExecuteFunction();

            Expression<Func<MessageReceiver, bool>> predicate = FilterService.GetExpression<MessageReceiver>(request.FilterGroup);
            //request.AddDefaultSortCondition(new SortCondition("CreatedTime", ListSortDirection.Descending));
            var page = CacheService.ToPageCache<MessageReceiver, MessageReceiverOutputDto>(MessageService.MessageReceivers, predicate, request.PageCondition, function);

            return page.ToPageData();
        }
        #endregion

        #region "新增消息"
        /// <summary>
        /// 新增消息
        /// </summary>
        /// <param name="dtos">消息DTO</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [ModuleInfo]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("新增")]
        public virtual async Task<AjaxResult> Create(params MessageInputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));

            var result = await MessageService.CreateMessages(dtos);
            return result.ToAjaxResult();
        }
        #endregion
        #region "更新消息"
        /// <summary>
        /// 更新消息
        /// </summary>
        /// <param name="dtos">消息DTO</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [ModuleInfo]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("更新")]
        public virtual async Task<AjaxResult> Update(params MessageInputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));

            var result = await MessageService.UpdateMessages(dtos);
            return result.ToAjaxResult();
        }
        #endregion
        #region "删除消息"
        /// <summary>
        /// 删除消息
        /// </summary>
        /// <param name="ids">消息编号</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [ModuleInfo]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("删除")]
        public virtual async Task<AjaxResult> Delete(params long[] ids)
        {
            Check.NotNull(ids, nameof(ids));

            var result = await MessageService.DeleteMessages(ids);
            return result.ToAjaxResult();
        }
        #endregion

        #region "发送消息"
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="ids">消息编号</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [ModuleInfo]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("发送")]
        public virtual async Task<AjaxResult> Send(params long[] ids)
        {
            Check.NotNull(ids, nameof(ids));

            var result = await MessageService.SendMessages(ids);
            return result.ToAjaxResult();
        }
        #endregion

        #endregion

    }
    #endregion

}
